/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer.sprites;

import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import javax.imageio.ImageIO;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.omg.CosNaming.IstringHelper;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.sprites.key.AbstractSheetKey;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.sprites.key.PawnSheetKey;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.sprites.key.TexturesSheetKey;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.sprites.sheetbuilder.PawnJSONLoader;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.exceptions.InvalidFileException;

/**
 * @author Massimo Canonaco
 *
 */
public class SpriteSheetCache 
{
	/** */
	static public String mapKey(final String race, final String frameName, final String step)
	{
		StringBuffer string = new StringBuffer(race.length()+frameName.length()+step.length()+step.length());
		string.append(race).append(POINT).append(frameName).append(POINT).append(step);
		
		return string.toString();
	}
	
	/** */
	private String raceFilesFolder;
	
	/** */
	private Map<AbstractSheetKey, SpriteSheetInterface> spritesInformations;
	
	/** */
	public SpriteSheetCache(String roleFile, Map<AbstractSheetKey, SpriteSheetInterface> map)
	{
		spritesInformations = map;
		raceFilesFolder = roleFile;
//		textureFilesFolder = textureFile;
	}
	
	public void add(AbstractSheetKey sheetKey, SpriteSheetInterface sheet)
	{
		spritesInformations.put(sheetKey, sheet);
	}
	
	//TEST 
		public Image getImage(AbstractSheetKey sheetkey, final int step, boolean light) throws InvalidFileException
		{
			SpriteSheetInterface sprite = spritesInformations.get(sheetkey);
			if(sheetkey instanceof PawnSheetKey)
			{
				PawnJSONLoader loader = null;
				PawnSheetKey pk = (PawnSheetKey) sheetkey;
				if(sprite == null)
				{
//					System.out.println("prima size "+raceFilesFolder);
					loader = new PawnJSONLoader();
					loader.build(light, raceFilesFolder, pk.getWho()+".json", pk, this);
//					System.out.println(pk.getKey());
//					System.out.println(pk.getFrameKey().getKey());
//					System.out.println("dopo size "+spritesInformations.size());

//					System.out.println(pk.getWhat());
					sprite = spritesInformations.get(pk);
//					if(sprite == null)
//					{
//						System.out.println(spritesInformations.keySet().size());
//						for (AbstractSheetKey a : spritesInformations.keySet()) {
////							System.out.println(a.getKey());
//						}
////						System.out.println("sprite ancora nullo o non trovato");
//						System.exit(0);
//					}
//					System.out.println(sprite.get);
				}
//				System.out.println(pk.getFrameKey().getKey());
				return sprite.getFrame(pk.getFrameKey().getKey());
				}
			else if(sheetkey instanceof TexturesSheetKey)
			{
				TexturesSheetKey tsk = (TexturesSheetKey) sheetkey;
				if(sprite != null)
				{
					return sprite.getFrame(tsk.getFrameKey().getKey());
				}
			}
				return null;
		}
		
		/**
		 * @throws InvalidFileException  */
		public Sprite getSpriteFrame(AbstractSheetKey sheetkey, boolean light) throws InvalidFileException 
		{
			SpriteSheetInterface spriteInterface = spritesInformations.get(sheetkey);
			if(sheetkey instanceof PawnSheetKey)
			{
				PawnJSONLoader loader = null;
				PawnSheetKey pk = (PawnSheetKey) sheetkey;
				if(spriteInterface == null)
				{
//					System.out.println("prima size "+raceFilesFolder);
					loader = new PawnJSONLoader();
					loader.build(light, raceFilesFolder, pk.getWho()+".json", pk, this);
//					System.out.println(pk.getKey());
//					System.out.println(pk.getFrameKey().getKey());
//					System.out.println("dopo size "+spritesInformations.size());

//					System.out.println(pk.getWhat());
					spriteInterface = spritesInformations.get(pk);
//					if(sprite == null)
//					{
//						System.out.println(spritesInformations.keySet().size());
//						for (AbstractSheetKey a : spritesInformations.keySet()) {
////							System.out.println(a.getKey());
//						}
////						System.out.println("sprite ancora nullo o non trovato");
//						System.exit(0);
//					}
//					System.out.println(sprite.get);
				}
				return spriteInterface.getSprite(sheetkey.getFrameKey().getKey());
				}
//			else if(sheetkey instanceof TexturesSheetKey)
//			{
//				TexturesSheetKey tsk = (TexturesSheetKey) sheetkey;
//				if(sprite != null)
//				{
//					return sprite.getFrame(tsk.getFrameKey().getKey());
//				}
//			}
				return null;
		}
	
	/** */
	static private final char POINT = '.';
	
	

}
